package com.ruoyi.blog.controller.api;

import com.github.pagehelper.PageHelper;
import com.ruoyi.blog.domain.BlogArticle;
import com.ruoyi.blog.domain.vo.ResultArticleDetailVo;
import com.ruoyi.blog.domain.vo.ResultArticleListVo;
import com.ruoyi.blog.service.IBlogArticleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.ip.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/blog/article")
public class ArticleController extends BaseController {

    @Autowired
    private IBlogArticleService articleService;

    /**
     * 查询博客列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getArticleList")
    public TableDataInfo getArticleList(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ResultArticleListVo> list = articleService.selectBlogArticleList();
        return getDataTable(list);
    }

    /**
     * 查询文章详情
     * @param blogId
     * @return
     */
    @GetMapping("getArticleDetail/{blogId}")
    public AjaxResult getArticleDetail(@PathVariable Long blogId, HttpServletRequest request) {
        articleService.viewArticle(blogId, IpUtils.getIpAddr(request));
        return AjaxResult.success(articleService.selectApiBlogArticleById(blogId) );
    }

    /**
     * 查询归档数据
     * @return
     */
    @GetMapping("getArchiveList")
    public AjaxResult getArchiveList() {
        return AjaxResult.success(articleService.getArchive() );
    }
}
